﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using He.Web.UI;
using He.Common;

namespace He.Web.manage.ad
{
    public partial class list : He.Web.UI.Manage
    {
        protected int pageSize = 12;
        protected int page;
        protected int pcount;
        public int position_id;
        public Model.ad_position mposition;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!int.TryParse(Request.Params["position_id"] as string, out this.position_id))
            {
                ShowParamError();
                return;
            }
            mposition = new BLL.ad_position().GetModel(position_id);
            page = GetInt("page", 0);
            if (!IsPostBack)
            {
                chkLoginLevel("viewAd");

                CookieHelper.Set("LastListUrl", Request.RawUrl);    //保存最后一次访问的列表页Url

                ShowInfo();
            }
        }

        #region 组合URL语句
        public string Comb_UrlTxt(int _position_id)
        {
            StringBuilder strTemp = new StringBuilder();
            strTemp.Append("&position_id=" + _position_id);
            return strTemp.ToString();
        }
        #endregion

        protected void ibtnRed_Command(object sender, CommandEventArgs e)
        {
            chkLoginLevel("editAd");
            Model.ad model = new BLL.ad().GetModel(Convert.ToInt32(e.CommandArgument));
            model.is_red = model.is_red == 1 ? 0 : 1;
            new BLL.ad().Update(model);
            //重写config.xml文件
            if (mposition.display_effect == Enums.FocusEffect.立体3D翻转.ToString())
            {
                WriteCu3erXml(mposition.id);
            }
            //更新缓存
            RemoveAdCache(mposition.call_mark);
            //保存日志
            SaveLogs("[广告管理]修改广告：" + model.title);
            Response.Redirect(Request.RawUrl);
        }

        private void ShowInfo()
        {
            BLL.ad bll = new BLL.ad();
            string strWhere = string.Format("position_id={0}", position_id);
            rptList.DataSource = bll.GetPageList(pageSize, page, strWhere, "sort_id asc,id desc");
            rptList.DataBind();
            pcount = bll.GetRecordCount(strWhere);

        }

        //删除
        protected void lbtnDel_Click(object sender, EventArgs e)
        {
            chkLoginLevel("delAd");

            BLL.ad bll = new BLL.ad();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("Check");
                int id = int.Parse(((Label)rptList.Items[i].FindControl("lb_id")).Text);
                if (cb.Checked)
                {
                    Model.ad model = bll.GetModel(id);
                    if (model.is_protected == 1)
                    {
                        JscriptMsg(350, 230, "操作失败", "<b>错误提示！</b>您要删除的数据受系统保护。", "back", "Error");
                        continue;
                    }
                    //删除图片
                    FileHelper.DeleteFile(model.img_url);
                    bll.Delete(id);
                }
            }
            //重写config.xml文件
            if (mposition.display_effect == Enums.FocusEffect.立体3D翻转.ToString())
            {
                WriteCu3erXml(mposition.id);
            }
            //更新缓存
            RemoveAdCache(mposition.call_mark);
            //保存日志
            SaveLogs("[广告管理]批量删除广告");

            ShowSuccess("批量删除成功啦！", Request.RawUrl);
        }

        //保存排序
        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            chkLoginLevel("editAd");

            BLL.ad bll = new He.BLL.ad();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("Check");
                int id = int.Parse(((Label)rptList.Items[i].FindControl("lb_id")).Text);
                int sortId;
                if (!int.TryParse(((TextBox)rptList.Items[i].FindControl("txtSortId")).Text.Trim(), out sortId))
                {
                    sortId = 99;
                }
                bll.UpdateField(id, "sort_id=" + sortId.ToString());
            }
            //重写config.xml文件
            if (mposition.display_effect == Enums.FocusEffect.立体3D翻转.ToString())
            {
                WriteCu3erXml(mposition.id);
            }
            //更新缓存
            RemoveAdCache(mposition.call_mark);
            //保存日志
            SaveLogs("[广告管理]批量保存排序");
            ShowSuccess("排序保存成功啦！", Request.RawUrl);
        }
    }
}
